package de.geckocraft.GeckoEventBroadcaster.Main;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.FileConfigurationOptions;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

public class MainConfig
{
  private static File file;
  private static FileConfiguration config;
  private static ArrayList<Integer> zusatzList = new ArrayList();
  static int intervallstart;
  static int intervall;
  static int intervallstop;
  static int changetext;

  public static void loadConfig()
  {
    Plugin mainplugin = Main.getPlugin();
    String filename = "config.yaml";
    file = new File(mainplugin.getDataFolder().getAbsolutePath(), filename);

    if (!file.exists())
    {
      try
      {
        file.getParentFile().mkdirs();
        file.createNewFile();
      }
      catch (IOException e) {
        file = null;
        return;
      }
      Bukkit.getServer().getLogger().info(filename + " erstellt");
    }

    try
    {
      config = YamlConfiguration.loadConfiguration(file);
      config.options().copyDefaults(true);
    }
    catch (Exception e) {
      Bukkit.getServer().getLogger().severe(e.toString());
    }
    addDefault();
    createZusatzList();
  }

  private static void addDefault()
  {
    config.addDefault("intervallstart", "2880");
    config.addDefault("intervall", "60");
    config.addDefault("intervallstop", "60");
    config.addDefault("zusatz1", "60");
    config.addDefault("zusatz2", "30");
    config.addDefault("zusatz3", "15");
    config.addDefault("zusatz4", "5");
    config.addDefault("zusatz5", "1");
    config.addDefault("changetext", "180");
    save();
  }

  private static void createZusatzList()
  {
    zusatzList.clear();
    for (int i = 1; i < 3600; i++)
    {
      String path = "zusatz" + i;
      String zusatzintervall = config.getString(path);
      int zusatzintervallint = 0;
      try
      {
        zusatzintervallint = Integer.parseInt(zusatzintervall);
      }
      catch (Exception localException)
      {
      }
      if (zusatzintervallint == 0)
      {
        break;
      }

      zusatzList.add(Integer.valueOf(zusatzintervallint));
    }

    String intervallstartString = getConfig().getString("intervallstart");
    String intervallString = getConfig().getString("intervall");
    String intervallstopString = getConfig().getString("intervallstop");
    String changetextString = getConfig().getString("changetext", "180");
    try
    {
      intervallstart = Integer.parseInt(intervallstartString);
      intervall = Integer.parseInt(intervallString);
      intervallstop = Integer.parseInt(intervallstopString);
      changetext = Integer.parseInt(changetextString);
    }
    catch (Exception localException1)
    {
    }
  }

  public static ArrayList<Integer> getZusatzList() {
    return zusatzList;
  }

  public static FileConfiguration getConfig()
  {
    return config;
  }

  public static int getintervall()
  {
    return intervall;
  }

  public static int getIntervallstart()
  {
    return intervallstart;
  }

  public static int getintervallstop()
  {
    return intervallstop;
  }

  public static int getchangetext()
  {
    return changetext;
  }

  protected static void save()
  {
    try
    {
      config.save(file);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}